﻿using dTools;
using System;
using System.Diagnostics;
using System.IO;

namespace Demo.PTGUI
{
    public partial class Sys
    {
        /// <summary>
        /// 根据程序名查找句柄
        /// </summary>
        /// <param name="processName">"PTGui"</param>
        /// <param name="findMultipleKill"></param>
        /// <returns></returns>
        public static IntPtr GetHandleByProcessName(string processName, bool findMultipleKill = false)
        {
            IntPtr handle = IntPtr.Zero;
            foreach (var process in Process.GetProcesses())
            {
                if (process.ProcessName == processName)
                {
                    if (handle == IntPtr.Zero)
                    {
                        handle = process.MainWindowHandle;
                    }
                    else
                    {
                        process.Kill();
                    }
                }
            }
            return handle;
        }

        public static void Log(string msg)
        {
            try
            {
                var log = FileHelper.Combine(AppDomain.CurrentDomain.BaseDirectory, "Log");
                File.AppendAllText(Path.Combine(log, $"{DateTime.Now:yyyyMMdd}.txt"), $"[{DateTime.Now:yyyyMMdd HH:mm:ss.fff}] {msg}{Environment.NewLine}");
            }
            catch
            {
            }
        }
    }
}
